package dawn.jxufe.community.controller.intercept;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author dawn21
 * @version 1.0
 * @date 2022/7/21 10:02
 * @component 拦截器示例
 */

// 拦截器示例。查看程序执行时机是否正确。
@Component
public class AlphaIntercept implements HandlerInterceptor {

    // org.slf4j
    private static final Logger logger = LoggerFactory.getLogger(AlphaIntercept.class);

    // 在controller之前执行功能本方法。
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("preHandle: " + handler.toString());
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    // 在controller之后，模板之前执行。
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // handler这个object是一个拦截的目标，一个方法。
        logger.debug("postHandle: " + handler.toString());
    }

    // 在TemplateEngine之后执行。
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("afterHandle: " + handler.toString());
    }
}
